package com.fire.social.service.impl;

import com.fire.social.entity.node.User;
import com.fire.social.mapper.FollowMapper;
import com.fire.social.mapper.UserMapper;
import com.fire.social.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FollowServiceImpl implements FollowService {

    @Autowired
    private UserMapper userMapper;

    public boolean contains(List<User> arr, User target) {
        for (User user : arr) {
            if (user.getId().equals(target.getId()))
                return true;
        }
        return false;
    }

    @Autowired
    private FollowMapper followMapper;

    @Override
    public Long createFollow(String account, String follower) {
        return followMapper.createFollow(account, follower);
    }

    @Override
    public Long deleteFollow(String account, String follower) {
        return followMapper.deleteFollow(account, follower);
    }

    @Override
    public void findFollowMe(User user, Map<String, Object> map) {
        List<User> follower = followMapper.getFollowMe(user.getAccount());
        List<User> myfollowing = userMapper.getMyFollow(user.getAccount());
        Set<User> recommends = new HashSet<>();
        Set<User> subFollowing = new HashSet<>();
        for (User value : follower) {
            if (contains(myfollowing, value)) {
                subFollowing.add(value);
                continue;
            }
            recommends.add(value);
        }
        Long myfollowNum = followMapper.countIFollow(user.getAccount());
        Long followMeNum = followMapper.countFollowMe(user.getAccount());
        map.put("myfollowing", myfollowNum);
        map.put("follower", followMeNum);
        map.put("recommends", recommends);
        map.put("mystar", subFollowing);
        map.put("user", user);
        map.put("title", "关注我的人");
        map.put("index", "首页");
        map.put("hascategory", true);
    }

    @Override
    public void findMyFollow(User user, Map<String, Object> map) {
        List<User> following = userMapper.getMyFollow(user.getAccount());
        Long myfollowNum = followMapper.countIFollow(user.getAccount());
        Long followMeNum = followMapper.countFollowMe(user.getAccount());
        map.put("myfollowing", myfollowNum);
        map.put("follower", followMeNum);
        map.put("recommends", following);
        map.put("user", user);
        map.put("title", "我关注的人");
        map.put("index", "首页");
    }
}
